package com.sq.imp;

import com.sq.abstractClass.MyMenu;
import com.sq.abstractClass.Text;
import com.sq.imp.MyMenuManagers.*;

import javax.swing.*;
import javax.swing.undo.UndoManager;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

public class MyMenuImp extends MyMenu {
    private final Text textArea;
    private final JFrame frame;

    public MyMenuImp(MainFrameImp mainFrameImp, Text textArea) {
        this.textArea = textArea;
        this.frame = mainFrameImp;

        // 初始化各个管理类
        FileManager fileManager = new FileManager(textArea, frame);
        EditManager editManager = new EditManager(textArea, frame);
        FindReplaceManager findReplaceManager = new FindReplaceManager(textArea, frame);
        FontStyleManager fontStyleManager = new FontStyleManager(textArea, frame);


        // 创建文件菜单
        JMenu fileMenu = new JMenu("文件");
        JMenuItem newFrameItem = new JMenuItem("新窗口");
        JMenuItem newFileItem = new JMenuItem("新建");
        JMenuItem openFileItem = new JMenuItem("打开");
        JMenuItem saveFileItem = new JMenuItem("保存");
        JMenuItem saveAsFileItem = new JMenuItem("另存为");
        JMenuItem exitItem = new JMenuItem("退出");


        fileMenu.add(newFrameItem);
        fileMenu.add(newFileItem);
        fileMenu.add(openFileItem);
        fileMenu.add(saveFileItem);
        fileMenu.add(saveAsFileItem);
        fileMenu.addSeparator();
        fileMenu.add(exitItem);

        newFrameItem.addActionListener(e -> MainFrameImp.createNewWindow());
        newFileItem.addActionListener(e -> fileManager.newFile());
        openFileItem.addActionListener(e -> fileManager.openFile());
        saveFileItem.addActionListener(e -> fileManager.saveFile());
        saveAsFileItem.addActionListener(e -> fileManager.saveFileAs());
        exitItem.addActionListener(e -> System.exit(0));
        newFileItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_DOWN_MASK));
        openFileItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_DOWN_MASK));
        saveFileItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_DOWN_MASK));
        saveAsFileItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK));
        // 创建编辑菜单
        JMenu editMenu = new JMenu("编辑");
        JMenuItem copyItem = new JMenuItem("复制");
        JMenuItem pasteItem = new JMenuItem("粘贴");
        JMenuItem cutItem = new JMenuItem("剪切");
        JMenuItem undoItem = new JMenuItem("撤回");
        JMenuItem redoItem = new JMenuItem("重做");
        JMenuItem findItem = new JMenuItem("查找");
        JMenuItem replaceItem = new JMenuItem("替换");
        JMenu viewMenu = new JMenu("查看");
        JMenuItem hexViewItem = new JMenuItem("以十六进制查看");
        JMenu skinMenu = new JMenu("皮肤");


        editMenu.add(copyItem);
        editMenu.add(pasteItem);
        editMenu.add(cutItem);
        editMenu.addSeparator();
        editMenu.add(undoItem);
        editMenu.add(redoItem);
        editMenu.addSeparator();
        editMenu.add(findItem);
        editMenu.add(replaceItem);
        viewMenu.add(hexViewItem);
        editMenu.add(viewMenu);
        editManager.createSkinMenuItems(skinMenu);
        viewMenu.add(skinMenu);


        copyItem.addActionListener(e -> editManager.copy());
        pasteItem.addActionListener(e -> editManager.paste());
        cutItem.addActionListener(e -> editManager.cut());
        undoItem.addActionListener(e -> editManager.undo());
        redoItem.addActionListener(e -> editManager.redo());
        hexViewItem.addActionListener(e->editManager.showHexEditor(frame, textArea.getText()));
        findItem.addActionListener(e -> findReplaceManager.showFindDialog());
        replaceItem.addActionListener(e -> findReplaceManager.showReplaceDialog());
        copyItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_DOWN_MASK));
        pasteItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_DOWN_MASK));
        cutItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_DOWN_MASK));
        undoItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z, InputEvent.CTRL_DOWN_MASK));
        redoItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Y, InputEvent.CTRL_DOWN_MASK));
        hexViewItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H, InputEvent.CTRL_DOWN_MASK));
        findItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, InputEvent.CTRL_DOWN_MASK));
        replaceItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, InputEvent.CTRL_DOWN_MASK));
        // 创建工具菜单
        JMenu toolMenu = new JMenu("工具");
        toolMenu.add(new DatabaseManager(textArea, frame).dbMenu);
        toolMenu.add(new CalculatorManager(textArea, frame).calculator);
//        toolMenu.add(new AudioPlayManagers(textArea, frame).audioPlay);
        // 添加菜单到菜单栏
        add(fileMenu);
        add(editMenu);

        // 添加字体样式菜单
        fontStyleManager.addStyleMenu(this);

        add(toolMenu);

        // 初始化UndoManager
        UndoManager undoManager = new UndoManager();
        textArea.getDocument().addUndoableEditListener(undoManager);

    }



}